package com.globallogic.vacationplanner.web.converter;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.globallogic.vacationplanner.transfer.dto.impl.UserDto;
import com.globallogic.vacationplanner.web.context.BackinBeanContext;
import com.globallogic.vacationplanner.web.controller.ProjectBean;

@FacesConverter("userListConverter")
public class UsersListConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		ProjectBean projectBean = new BackinBeanContext()
				.findBean("projectBean");
		if (value.trim().equals("")) {
			return null;
		} else {
			List<UserDto> users = projectBean.getUsers();
			for (UserDto userDto : users) {
				if (userDto.getLogin().equals(value)) {
					return userDto;
				}
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		return ((UserDto) value).getLogin();
	}

}
